//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.configuration.mqtt;

import javax.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.messaging.MessageChannel;

@Configuration
public class MqttOutboundConfiguration {
    @Resource
    private MqttProperties mqttProperties;

    public MqttOutboundConfiguration() {
    }

    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        String password = "password";
        String[] serverURIs = this.mqttProperties.getOutbound().getUrls().split(",");
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setServerURIs(serverURIs);
        factory.setCleanSession(false);
        factory.setUserName("admin");
        factory.setPassword(password);
        return factory;
    }

    @Bean
    @ServiceActivator(
            inputChannel = "mqttOutboundChannel"
    )
    public LocalMessageHandler mqttOutbound() {
        LocalMessageHandler messageHandler = new LocalMessageHandler(this.mqttProperties.getOutbound().getClientId(), this.mqttClientFactory());
        messageHandler.setAsync(true);
        messageHandler.setDefaultTopic(this.mqttProperties.getOutbound().getTopic());
        messageHandler.setAsyncEvents(true);
        messageHandler.setDefaultRetained(false);
        return messageHandler;
    }

    @Bean
    public MessageChannel mqttOutboundChannel() {
        return new DirectChannel();
    }
}
